package a66weding.com.jiehuntong.activity.home.hotel.shop;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.andview.refreshview.XRefreshView;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HomeBotomListBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.inner.ToNextInnner;

/**
 * Created by admin on 2016/9/14.
 */
public class HotPackageFragment extends Fragment implements OnNetResultListener{

    private ToNextInnner innner;
    private int i_category;
    public void setInnner(ToNextInnner innner) {
        this.innner = innner;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.fragment_hotpackage,null);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        i_category = ((HomeProductdetailActivity)getActivity()).getI_category();
        page = 1;
        getDataFromServerFor(page);
    }

    private void initView(View view){

        initRefreshView(view);
    }

    private XRefreshView xRefreshView;
    private ListView list;
    private boolean isDownRefresh = true;
    private  HotPackageAdapter adapter;
    private int page=1;
    private void initRefreshView(View view) {
        xRefreshView = (XRefreshView) view.findViewById(R.id.help_xrefreshview);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(true);
        // 设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);
        // 静默加载模式不能设置footerview
        // 设置支持自动刷新
        xRefreshView.setAutoLoadMore(true);
        //listView
        list = (ListView) view.findViewById(R.id.fragment_shop_tuwen_list);
        adapter = new HotPackageAdapter(getContext(),null);
        list.setAdapter(adapter);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh() {
                isDownRefresh = true;
                if (innner!=null){
                    innner.toNext();
                }
                xRefreshView.stopRefresh();
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                isDownRefresh = false;
                ++page;
                getDataFromServerFor(page);
            }
        });
    }

    private List<HomeBotomListBean.DataBean> bottomlist;
    @Override
    public void onSucess(SuccessBean sucessbean) {
        if (isDownRefresh){
            xRefreshView.stopRefresh();
        }else{
            xRefreshView.stopLoadMore();
        }
        Object data = sucessbean.getData();
        if (data instanceof HomeBotomListBean){
            HomeBotomListBean dataBean = (HomeBotomListBean) data;
            switch (dataBean.getResCode()){
                case "0000":
                    if (isDownRefresh){
                        adapter.clear();
                    }
                    bottomlist = dataBean.getData();
                    adapter.append(bottomlist);
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

        private void getDataFromServerFor(int pageIndex) {
        String url;
        if (((HomeProductdetailActivity)getActivity()).isLogin()) {
            url = RequestCode.GET_PRODUCTLISTDETAIL+"&categoryID="+i_category+"&pageIndex="+pageIndex+"&memberID="+
                    DataWareHourse.getInstance().getmUser().getData().get(0).getI_Id();
        } else {
            url = RequestCode.GET_PRODUCTLISTDETAIL+"&categoryID="+i_category+"&pageIndex="+pageIndex+"&memberID="+ 0;
        }
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.GETREQUEST, url,
                this, HomeBotomListBean.class, RequestCode.GET_PRODUCTLISTDETAIL);

        HttpHandleUtil.executeCommonGet(getContext(),httpRequestBean);
    }

}
